package com.example.pictureManage.service.imageDetailService;

import com.example.pictureManage.service.mainViewStage;
import javafx.animation.AnimationTimer;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片播放器
 * @author 郑伯洋
 * 用于保存图片的前进和向后
 */
public class imagePlayer {
    //获取打开的目录树
    private final List<URI> URIList = new ArrayList<>();

    private AnimationTimer animationTimer;

    public AnimationTimer getAnimationTimer() {
        return animationTimer;
    }

    public imagePlayer(){
        //获取到图片预览下的图片集合
        var nodes = mainViewStage.getTilePane().getChildren();
        for (var item : nodes){
            try {
                URIList.add(new URI((String) item.getUserData()));
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //播放下一张图片
    public void imageNext(ImageView imageView) {
        //获取到当前的索引
        int index = getIndex(imageView);
        //合法
        if (0 <= index && index < URIList.size()-1) {
            //设置为下一张
            Image image = new Image(URIList.get(index + 1).toString());
            //设置阶段
            imageView.setImage(image);
            imageView.setFitHeight(600);
            imageView.setFitWidth(800);
            imageView.setTranslateX(0);
            imageView.setTranslateY(0);
            imageView.setRotate(0);
        }
        else{
            if (animationTimer != null) {
                animationTimer.stop();
            }
            //提示框提示
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示:");
            alert.setHeaderText(null);
            alert.setContentText("已经是最后一张了");
            alert.show();
        }
    }

    //切换到上一张图片
    public void imagePre(ImageView imageView) {
        //获取到当前的索引
        int index = getIndex(imageView);
        System.out.println(index);
        //合法
        //设置成上一张
        if (0 < index && index < URIList.size()) {
            Image image = new Image(URIList.get(index - 1).toString());
            //设置阶段
            imageView.setImage(image);
            imageView.setFitHeight(600);
            imageView.setFitWidth(800);
        }
        else{
            //提示框提示
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setHeaderText(null);
            alert.setContentText("已经是第一张了");
            alert.show();
        }
    }

    //播放幻灯片
    public void imagePlay(ImageView imageView) {
        //每次点击播放都要先停止之前的播放
        imageStop();
        //设定时长
        long INTERVAL = 3_000_000_000L;
        final long[] lastUpdate = {0};
        animationTimer = new AnimationTimer() {
            int cur = 0;
            @Override
            public void handle(long now) {
                if (now - lastUpdate[0] >= INTERVAL){
                    imageNext(imageView);
//                    System.out.println(now+","+lastUpdate[0]);
                    lastUpdate[0] = now;
                }
                //第一次会被迫执行imageNext  ：是因为now一开始就是非零的一个数字所以会被迫执行
                //所以要取消这个主动切换到下一个图片的问题
                if (cur == 0){
                    imagePre(imageView);
                    cur++;
                }
            }
        };
        animationTimer.start();
    }

    public void imageStop(){
        if (animationTimer != null){
            animationTimer.stop();
        }
    }
    /**
     * 获取到当前文件的索引
     * */
    private int getIndex(ImageView imageView){
        int index = 0;
        //获取到当前的索引
        String curURL = imageView.getImage().getUrl();
        for (int i = 0;i < URIList.size();i++){
            try {
                if (curURL.equals(URIList.get(i).toURL().toString())){
                    index = i;
                    break;
                }
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }
        return index;
    }
}
